package java44th.group.controller.admin.keyword;

import java44th.group.bean.common.BasePageInfo;
import java44th.group.bean.common.CommonData;
import java44th.group.bean.po.MarketKeyword;
import java44th.group.bean.admin.vo.BaseRespVo;
import java44th.group.bean.admin.vo.keyword.CreateKeywordVo;
import java44th.group.service.admin.keyword.KeywordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 小焦
 * @since 2022/10/13 00:23
 */

@RestController
@RequestMapping("admin/keyword")
public class KeywordController {
    @Autowired
    KeywordService keywordService;


    @GetMapping("list")
    public BaseRespVo list(BasePageInfo info, String keyword, String url) {
        CommonData<MarketKeyword> data = keywordService.list(info, keyword, url);
        return BaseRespVo.ok(data);
    }

    /**
     * 1. 获取请求参数--{"keyword":"77","url":"7k7k","isHot":false,"isDefault":false}
     * 2. 处理业务逻辑--向keyword数据库中添加一条记录
     * 3. 返回响应--{"id":72,"keyword":"77","url":"7k7k","isHot":false,"isDefault":false,"addTime":"2022-10-13 11:01:53","updateTime":"2022-10-13 11:01:53"}
     *
     * @return java44th.group.bean.admin.vo.BaseRespVo
     * @author 小焦
     * @since 2022/10/13 上午 11:02
     */
    @PostMapping("create")
    public BaseRespVo create(@RequestBody MarketKeyword marketKeyword) {
        CreateKeywordVo keyword = null;
        try {
            keyword = keywordService.create(marketKeyword);
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.error(402, "参数值不对");
        }
        return BaseRespVo.ok(keyword);
    }

    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody MarketKeyword keyword) {
        int code= 0;
        try {
            code = keywordService.delete(keyword);
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.error(402, "参数值不对");
        }
        if (code==1){
            return BaseRespVo.ok();
        }
        return BaseRespVo.invalidParameter("删除失败");
    }
    @PostMapping("update")
    public  BaseRespVo update(@RequestBody MarketKeyword keyword){
        int code = 0;
        try {
            code = keywordService.update(keyword);
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.error(402, "参数值不对");
        }
        if (code==1){
            return BaseRespVo.ok();
        }
        return BaseRespVo.invalidParameter("删除失败");
    }
}
